<?php

namespace Biz\model\wiki;


use Doraemon\tools\ImgUrl;

class WikiBrandAreaAd extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'wiki_brand_area_ad';

    const AD_TYPE_VIDEO = 1;
    const AD_TYPE_PIC = 2;
    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;
    const ADM_AD_TYPE_MAP = [
        self::AD_TYPE_VIDEO => '视频',
        self::AD_TYPE_PIC => '图片',
    ];

    public static function getTypeName($type, $default = '')
    {
        return self::ADM_AD_TYPE_MAP[$type] ?? $default;
    }

    public static function admModuleList($brand_id)
    {
        $where = [
            'brand_id' => $brand_id,
            'ad_status' => self::STATUS_NORMAL,
            'ORDER' => ['sort' => 'DESC']
        ];
        $list = self::select('*', $where);
        if ($list) {
            $list = self::admFormat($list);
        }
        return $list;
    }

    public static function admFormat($list)
    {
        $response = [];
        foreach ($list as $v) {
            $v['img_url'] = $v['img'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $v['img'], ImgUrl::IMAGE_SIZE_OP_ORI) : '';
            $v['ad_type_desc'] = self::getTypeName($v['ad_type'], '-');;
            $response[] = $v;
        }
        return $response;
    }

    public static function admGetInfo($id)
    {
        $where = [
            'id' => $id,
        ];
        return self::get('*', $where);
    }
    public static function admDelModule($id)
    {
        $where = [
            'id' => $id,
        ];
        $data['edittime'] = date("Y-m-d H:i:s");
        $data['ad_status'] = self::STATUS_DELETE;
        return self::update($data, $where)->rowCount();
    }

    public static function admSaveInfo($id, $data)
    {
        if (self::admGetInfo($id)) {
            $where = [
                'id' => $id,
            ];
            $data['edittime'] = date("Y-m-d H:i:s");
            return self::update($data, $where)->rowCount();
        } else {
            $data['ad_status'] = self::STATUS_NORMAL;
            $data['addtime'] = date("Y-m-d H:i:s");
            self::insert($data);
            return self::id();
        }
    }
    
    public static function getAllByWhere($where = [],$files = '*')
    {
        if(!empty($where)){
            $where = array_merge($where, ['ad_status'=>self::STATUS_NORMAL]);
        }else{
            $where =  ['ad_status'=>self::STATUS_NORMAL];
        }
        $data = self::select($files, $where);
        if(!empty($data)) {
            $data = array_map(function ($_data) {
                return self::_filter($_data);
            }, $data);
        }
        return $data;
    }

    public static function _filter($data){
        if($data['ad_type'] == self::AD_TYPE_PIC) {
            foreach ($data as $_k => &$_v) {
                switch ($_k) {
                    case 'img':
                        $data['img_url'] = !empty($_v) ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $_v, ImgUrl::IMAGE_SIZE_OP_ORI) : '';
                        break;
                }
            }
        }
        return $data;
    }

    public static function getOneByWhere( $where = [], $files = '*')
    {
        $data = self::get($files, $where);
        return $data;
    }

    public static function addData($data)
    {
        try {
            self::insert($data);
            return self::id();
        } catch (Exception $e) {
            return false;
        }
    }

    public static function updateData($data, $where)
    {
        try {
            self::update($data, $where);
            return true;
        } catch (Exception $e) {
            return false;
        }
    }
}